﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Reflection;

namespace BiaoYinTest
{
    public interface IBlock
    {
        string Text { get; }

        bool IsSelected { get; set; }

        double ZIndex { get; }

        string Points { get; }

        string RealPoints { get; }

        Brush StrokeBrush { get; }

        void SetRate(double rate);

        Rect Rect { get; }

        ICollection<IBlock> Items { get; }

        void updatePoints();

        void UpdatePoints(PointCollection points);

        XElement ToXElement();

        string Url { get; }

        string Author { get; set; }
    }

    //public enum ArticProperty { 文章块, 图文说明, 转换为图片, 文字打印到图片 }
    //public enum NextPageType { 无转版, 下转, 上接 }
    //public enum ArticOrderType { 按坐标位置排序, 按合并顺序排序 }

    public class ArticInfo : IBlock, System.ComponentModel.INotifyPropertyChanged
    {
        public void UpdatePoints(PointCollection points)
        {
            throw new NotImplementedException();
        }

        public ArticInfo(double rate)
        {
            this.rate = rate;
        }

        Content title = new Content();

        public Content Title
        {
            get { return title; }
            set { title = value; }
        }

        Content subTitle = new Content();

        public Content SubTitle
        {
            get { return subTitle; }
            set { subTitle = value; }
        }

        Content introTitle = new Content();
        public Content IntroTitle
        {
            get { return introTitle; }
            set { introTitle = value; }
        }

        public string Contents { get; set; }

        string author = string.Empty;

        /// <summary>
        /// 作者
        /// </summary>
        public string Author
        {
            get { return author; }
            set { author = value; raisePropertyChanged("Author"); }
        }

        ContentInfo articType = new ContentInfo();
        /// <summary>
        /// 稿件类型
        /// </summary>
        public ContentInfo ArticType
        {
            get { return articType; }
            set { articType = value; raisePropertyChanged("ArticType"); }
        }

        ContentInfo articSource = new ContentInfo();
        /// <summary>
        /// 稿件来源
        /// </summary>
        public ContentInfo ArticSource
        {
            get { return articSource; }
            set { articSource = value; raisePropertyChanged("ArticSource"); }
        }

        ContentInfo nextPage = new ContentInfo();
        /// <summary>
        /// 转版
        /// </summary>
        public ContentInfo NextPage
        {
            get { return nextPage; }
            set { nextPage = value; raisePropertyChanged("NextPage"); }
        }

        public string Url
        {
            get
            {
                return Images.ToString();
            }
        }

        ImageCollection images = new ImageCollection();

        Image Image
        {
            get
            {
                if (images.Count > 0)
                {
                    return (Image)images[0];
                }
                return null;
            }
        }

        public ImageCollection Images { get { return images; } }

        ContentInfo articProperty = new ContentInfo();

        public ContentInfo ArticProperty
        {
            get { return articProperty; }
            set
            {
                articProperty = value;

                raisePropertyChanged("ArticProperty");
            }
        }

        Rect rect;

        public Rect Rect { get { return rect; } }

        bool isSelected = false;

        public bool IsReadOnly
        {
            get
            {
                return items.Count != 1 || !(items[0] is ArticInfo);
            }
        }

        ///// <summary>
        ///// 合并
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //public IBlock Combine()
        //{
        //    //计算rect

        //    double xLeft = 0, xRight = 0, yTop = 0, yBottom = 0;

        //    int index = 0;
        //    foreach (IBlock i in items)
        //    {
        //        if (index == 0)
        //        {
        //            xLeft = i.Rect.Left;
        //            xRight = i.Rect.Right;
        //            yTop = i.Rect.Top;
        //            yBottom = i.Rect.Bottom;
        //        }
        //        else
        //        {
        //            xLeft = xLeft < i.Rect.Left ? xLeft : i.Rect.Left;
        //            xRight = xRight > i.Rect.Right ? xRight : i.Rect.Right;
        //            yTop = yTop < i.Rect.Top ? yTop : i.Rect.Top;
        //            yBottom = yBottom > i.Rect.Bottom ? yBottom : i.Rect.Bottom;
        //        }
        //        index++;
        //    }

        //    this.rect = new Rect(new Point(xLeft, yTop), new Point(xRight, yBottom));
        //    points.Clear();
        //    points.Add(new Point(xLeft * rate, yTop * rate));
        //    points.Add(new Point(xRight * rate, yTop * rate));
        //    points.Add(new Point(xRight * rate, yBottom * rate));
        //    points.Add(new Point(xLeft * rate, yBottom * rate));

        //    return this;
        //}

        //public void Clear()
        //{
        //    Title = null;
        //    SubTitle = null;
        //    IntroTitle = null;
        //    Contents = null;
        //    images.Clear();

        //    foreach (IBlock i in items)
        //    {
        //        i.IsSelected = false;
        //    }
        //    items.Clear();

        //    updateContents();
        //    raisePropertyChanged("IsReadOnly");
        //}

        //private ArticInfo Colone()
        //{
        //    throw new NotImplementedException();
        //}

        #region INotifyPropertyChanged 成员

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        #endregion

        List<IBlock> items = new List<IBlock>();

        public ICollection<IBlock> Items
        {
            get { return items; }
        }

        //public void Add(IBlock block)
        //{
        //    items.Add(block);
        //    updateContents();

        //    if (items.Count == 1)
        //    {
        //        PropertyInfo pInfo = items.First().GetType().GetProperty("ArticProperty");
        //        if (pInfo != null)
        //        {
        //            ArticProperty = (ArticProperty)pInfo.GetValue(items.First(), null);
        //        }
        //    }

        //    raisePropertyChanged("IsReadOnly");
        //}

        //public void Remove(IBlock block)
        //{
        //    items.Remove(block);

        //    updateContents();

        //    raisePropertyChanged("IsReadOnly");
        //}

        //protected void updateContents()
        //{
        //    Title = null;
        //    SubTitle = null;
        //    IntroTitle = null;

        //    Images.Clear();

        //    StringBuilder contents = new StringBuilder();
        //    StringBuilder sbUrls = new StringBuilder();

        //    foreach (IBlock b in items)
        //    {
        //        switch (b.GetType().Name)
        //        {
        //            case "Image":
        //                {
        //                    Images.Add(b);
        //                    sbUrls.Append(b.Url);
        //                }
        //                break;
        //            case "Title":
        //                {
        //                    Title bT = b as Title;

        //                    if (this.Title == null)
        //                    {
        //                       this.Title = new Content(bT.Text,bT.FontHeight);

        //                       if (bT.SubTitle != null)
        //                       {
        //                           this.SubTitle = new Content(bT.SubTitle.Text, bT.SubTitle.FontHeight);
        //                       }
        //                    }
        //                    else
        //                    {
        //                        //字体较大的为引题
        //                        if (this.Title.FontHeight > bT.FontHeight)
        //                        {
        //                            //设置引题
        //                            this.IntroTitle = new Content(this.Title.Text, this.Title.FontHeight);

        //                            //设置标题
        //                            if (this.SubTitle != null)
        //                            {
        //                                this.Title = new Content(this.SubTitle.Text, this.SubTitle.FontHeight);

        //                                //设置副题

        //                            }
        //                            else
        //                            {
        //                                this.Title = new Content(bT.Text, bT.FontHeight);

        //                                if (bT.SubTitle != null)
        //                                {
        //                                    this.SubTitle = new Content(bT.SubTitle.Text, bT.SubTitle.FontHeight);
        //                                }
        //                            }

        //                        }
        //                        else
        //                        {
        //                            this.IntroTitle = new Content(bT.Text,bT.FontHeight);
        //                        }
        //                    }

        //                }
        //                break;
        //            case "Block":
        //                {
        //                    contents.AppendLine(b.Text);
        //                }
        //                break;
        //            case "ArticInfo":
        //                {
        //                    ArticInfo a = b as ArticInfo;

        //                    switch (a.ArticProperty)
        //                    {
        //                        case ArticProperty.文章块:
        //                            {
        //                                Title = a.Title;
        //                                SubTitle = a.SubTitle;
        //                                IntroTitle = a.IntroTitle;
        //                                Images.AddRange(a.Images);
        //                                contents.AppendLine(a.Contents);
        //                                sbUrls.Append(a.Url);
        //                            }
        //                            break;
        //                        case ArticProperty.图文说明:
        //                            {
        //                                Images.Add(a);
        //                                sbUrls.Append(a.Url);

        //                                if (items.Count == 1)
        //                                {
        //                                    contents.AppendLine(a.Contents);
        //                                }
        //                                //contents.AppendLine(a.Contents);
        //                            }
        //                            break;
        //                        default:
        //                            break;
        //                    }

        //                }
        //                break;
        //            default:
        //                break;
        //        }
        //    }

        //    Contents = contents.ToString();
        //    Url = sbUrls.ToString();

        //    if (items.Count == 1)
        //    {
        //        ArticProperty = Helpers.ReflectHelper.GetProperty<ArticProperty>(items.First(), "ArticProperty");
        //        Author = Helpers.ReflectHelper.GetProperty<string>(items.First(), "Author");
        //    }
        //    else
        //    {
        //        ArticProperty = ArticProperty.文章块;
        //        Author = string.Empty;
        //    }

        //    raisePropertyChanged("Title");
        //    raisePropertyChanged("SubTitle");
        //    raisePropertyChanged("IntroTitle");
        //    raisePropertyChanged("Images");
        //    raisePropertyChanged("Contents");
        //    raisePropertyChanged("Url");
        //}

        private void raisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }

        MyPointCollection points = new MyPointCollection();


        #region IBlock 成员

        public string Text
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                if (!string.IsNullOrEmpty(this.IntroTitle.Text))
                {
                    sb.AppendLine(this.IntroTitle.Text);
                }

                if (!string.IsNullOrEmpty(this.Title.Text))
                {
                    sb.AppendLine(this.Title.Text);
                }

                if (!string.IsNullOrEmpty(this.SubTitle.Text))
                {
                    sb.AppendLine(this.SubTitle.Text);
                }

                if (!string.IsNullOrEmpty(this.Contents))
                {
                    sb.AppendLine(this.Contents);
                }


                return sb.ToString();
            }
        }

        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                isSelected = value;
                raisePropertyChanged("StrokeBrush");
            }

        }

        public double ZIndex
        {
            get { return rect.Width * rect.Height; }
        }

        public string Points
        {
            get { return points.ToString(); }
        }

        MyPointCollection realPoints = new MyPointCollection();

        public string RealPoints
        {
            get
            {
                return realPoints.ToString();
            }
        }

        public Brush StrokeBrush
        {
            get
            {
                if (isSelected)
                {
                    return new SolidColorBrush(Colors.Red);
                }
                else
                {

                    switch ((ArticPropertyEnum) ContentInfo.GetInt32Id(ArticProperty))
                    {
                        case  ArticPropertyEnum.文章块:
                            return new SolidColorBrush(Colors.Green);
                        case  ArticPropertyEnum.图文说明:
                            return new SolidColorBrush(Colors.Yellow);
                        default:
                            return new SolidColorBrush(Colors.Purple);

                    }
                }
            }
        }


        #region 生成XML
        public virtual XElement ToXElement()
        {
            switch ((ArticPropertyEnum)ContentInfo.GetInt32Id( ArticProperty))
            {
                case ArticPropertyEnum.文章块://稿件
                    {

                        XElement xArticle = new XElement("Article");
                        xArticle.SetAttributeValue("Detail", "true");
                        xArticle.SetAttributeValue("Id", "8");
                        xArticle.SetAttributeValue("Type", "");
                        xArticle.SetAttributeValue("AdId", "");
                        xArticle.SetAttributeValue("ArticSource", ArticSource);
                        xArticle.SetAttributeValue("ArticSourceId", ContentInfo.GetId(ArticSource));
                        xArticle.SetAttributeValue("ArticType", ArticType);
                        xArticle.SetAttributeValue("ArticTypeId", ContentInfo.GetId(ArticType));
                        xArticle.SetAttributeValue("NextPage", NextPage);
                        xArticle.SetAttributeValue("NextPageId", ContentInfo.GetId(NextPage));
                        xArticle.SetAttributeValue("NextPage", NextPage);
                        xArticle.SetAttributeValue("NextPageId", ContentInfo.GetId(NextPage));
                        xArticle.SetAttributeValue("ArticProperty", ArticProperty);
                        xArticle.SetAttributeValue("ArticProperty", ContentInfo.GetId(ArticProperty));

                        if (IntroTitle != null)
                        {
                            XElement xIntroTitle = new XElement("IntroTitle", new XCData(IntroTitle.Text));
                            xArticle.Add(xIntroTitle);
                        }

                        XElement xTitle = new XElement("Title", new XCData(Title.Text));
                        xTitle.SetAttributeValue("Font", "微软雅黑");
                        xTitle.SetAttributeValue("Size", "36");
                        xTitle.SetAttributeValue("Bold", "true");
                        xTitle.SetAttributeValue("Italic", "false");
                        xTitle.SetAttributeValue("Align", "justify");
                        xTitle.SetAttributeValue("LetterSpacing", "-2");
                        xTitle.SetAttributeValue("Leading", "-6");
                        xTitle.SetAttributeValue("Txt", "");
                        xArticle.Add(xTitle);

                        XElement xSubTitle = new XElement("SubTitle", new XCData(SubTitle.Text));
                        xArticle.Add(xSubTitle);

                        XElement xAuthor = new XElement("Author", Author);
                        xArticle.Add(xAuthor);

                        XElement xContent = new XElement("Content", new XCData(Contents));
                        xContent.SetAttributeValue("Font", "方正报宋简体");
                        xContent.SetAttributeValue("Size", "16");
                        xContent.SetAttributeValue("Bold", "true");
                        xContent.SetAttributeValue("Italic", "false");
                        xContent.SetAttributeValue("Align", "left");
                        xContent.SetAttributeValue("LetterSpacing", "0");
                        xContent.SetAttributeValue("Leading", "8");
                        xArticle.Add(xContent);

                        XElement xImageList = new XElement("ImageList");
                        xImageList.SetAttributeValue("ID", "0");
                        xImageList.SetAttributeValue("X", "0");
                        xImageList.SetAttributeValue("Y", "0");

                        xArticle.Add(xImageList);

                        foreach (IBlock b in Images)
                        {
                            xImageList.Add(b.ToXElement());
                        }

                        XElement xPointList = new XElement("PointList");
                        foreach (Point pt in realPoints)
                        {
                            xPointList.Add(new XElement("Point", new XAttribute("X", pt.X), new XAttribute("Y", pt.Y)));
                        }
                        xArticle.Add(xPointList);



                        XElement xSubItems = new XElement("Items");
                        xArticle.Add(xSubItems);

                        foreach (IBlock b in items)
                        {
                            xSubItems.Add(b.ToXElement());
                        }
                        return xArticle;
                    }
                case ArticPropertyEnum.图文说明://图文说明
                    {
                        XElement xImage = new XElement("ArticImage", new XCData(this.Text));
                        xImage.SetAttributeValue("PathName", Url);
                        xImage.SetAttributeValue("X", "0");
                        xImage.SetAttributeValue("Y", "0");
                        xImage.SetAttributeValue("Author", Author);

                        XElement xSubItems = new XElement("Items");
                        xImage.Add(xSubItems);

                        foreach (IBlock b in items)
                        {
                            xSubItems.Add(b.ToXElement());
                        }

                        return xImage;
                    }
                case  ArticPropertyEnum.转换为图片://ArticProperty.转换为图片:
                    break;
                case  ArticPropertyEnum.文字打印到图片://ArticProperty.文字打印到图片:
                    break;
                default:
                    break;
            }

            return null;
        }
        #endregion

        double rate;

        public void SetRate(double rate)
        {
            this.rate = rate;
            updatePoints();
        }

        #endregion

        public void updatePoints()
        {
            double xLeft = double.NaN;
            double xRight = double.NaN;
            double yTop = double.NaN;
            double yBottom = double.NaN;

            int index = 0;
            foreach (IBlock i in items)
            {
                if (index == 0)
                {
                    xLeft = i.Rect.Left;
                    xRight = i.Rect.Right;
                    yTop = i.Rect.Top;
                    yBottom = i.Rect.Bottom;
                }
                else
                {
                    xLeft = xLeft < i.Rect.Left ? xLeft : i.Rect.Left;
                    xRight = xRight > i.Rect.Right ? xRight : i.Rect.Right;
                    yTop = yTop < i.Rect.Top ? yTop : i.Rect.Top;
                    yBottom = yBottom > i.Rect.Bottom ? yBottom : i.Rect.Bottom;
                }

                index++;
            }

            rect = new Rect(new Point(xLeft, yTop), new Point(xRight, yBottom));

            points.Clear();
            realPoints.Clear();

            realPoints.Add(new Point(rect.Left, rect.Top));
            realPoints.Add(new Point(rect.Right, rect.Top));
            realPoints.Add(new Point(rect.Right, rect.Bottom));
            realPoints.Add(new Point(rect.Left, rect.Bottom));

            points.Add(new Point(rect.Left * rate, rect.Top * rate));
            points.Add(new Point(rect.Right * rate, rect.Top * rate));
            points.Add(new Point(rect.Right * rate, rect.Bottom * rate));
            points.Add(new Point(rect.Left * rate, rect.Bottom * rate));

            raisePropertyChanged("Points");
        }

    }

    public class Title : Block, System.ComponentModel.INotifyPropertyChanged
    {
        public Title(string path)
            : base(path, "title")
        {
        }

        public Content SubTitle
        {
            get { return contents[1]; }
        }

        public Content IntroTitle
        {
            get { return null; }
        }

        public int FontHeight
        {
            get { return contents[0].FontHeight; }
        }

        public override string Text
        {
            get
            {
                return contents[0].Text;
            }
            set
            {
                contents[0].Text = value;
            }
        }

        public override XElement ToXElement()
        {
            XElement xBlock = new XElement("BlockInfo");
            xBlock.SetAttributeValue("Type", "title");
            xBlock.SetAttributeValue("Path", RealPoints);

            if (IntroTitle != null)
            {
                XElement xIntroTitle = new XElement("IntroTitle", new XCData(IntroTitle.Text));
                xIntroTitle.SetAttributeValue("FontHeight", IntroTitle.FontHeight);
                xBlock.Add(xIntroTitle);
            }

            XElement xTitle = new XElement("Title", new XCData(Text));
            xTitle.SetAttributeValue("FontHeight", FontHeight);
            xBlock.Add(xTitle);

            if (SubTitle != null)
            {
                XElement xSubTitle = new XElement("SubTitle", new XCData(SubTitle.Text));
                xSubTitle.SetAttributeValue("FontHeight", SubTitle.FontHeight);
                xBlock.Add(xSubTitle);
            }

            return xBlock;
        }
    }

    public class Image : Block, System.ComponentModel.INotifyPropertyChanged
    {
        public Image(string path, string url)
            : base(path, "Image")
        {
            this.Url = url;
        }

        public Image(string url)
            : this("", url)
        {
        }

        string text = string.Empty;
        public override string Text
        {
            get { return text; }
            set { text = value; OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Text")); }
        }

        public override Brush StrokeBrush
        {
            get
            {
                if (IsSelected)
                {
                    return new SolidColorBrush(Colors.Red);
                }
                else
                {
                    return new SolidColorBrush(Colors.Yellow);
                }
            }
        }



        public override XElement ToXElement()
        {
            XElement xImage = new XElement("Image", new XCData(Text));
            xImage.SetAttributeValue("PathName", Url);
            xImage.SetAttributeValue("X", Rect.Left);
            xImage.SetAttributeValue("Y", Rect.Top);
            xImage.SetAttributeValue("Author", Author);

            return xImage;
        }

    }

    public abstract class Block : System.ComponentModel.INotifyPropertyChanged, IBlock
    {
        string firstType;
        double rate = 1;
        MyPointCollection realPoints = new MyPointCollection();

        public void UpdatePoints(PointCollection points)
        {
            realPoints.Clear();
            FormatePoints(points);
            foreach (Point pt in points)
            {
                Point realPt = new Point(pt.X / rate, pt.Y / rate);
                realPoints.Add(realPt);
            }

            updatePoints();
        }

        void FormatePoints(PointCollection points)
        {
            List<Point> pts = new List<Point>();
            //去除多余的店
            if (points.Count > 3)
            {
                Point lastPoint = points[0];
                Point currentPoint = points[1];

                pts.Add(lastPoint);

                Point nextPoint = points[2];

                for (int ni = 2; ni < points.Count; ni++)
                {
                    nextPoint = points[ni];
                    //如果三个点X坐标相同，移除中间的点
                    if (isInLine(lastPoint, currentPoint, nextPoint))
                    {
                        currentPoint = nextPoint;
                    }
                    else
                    {
                        lastPoint = currentPoint;
                        pts.Add(currentPoint);
                        currentPoint = nextPoint;
                    }
                }
            }

            points.Clear();


            foreach (Point pt in pts)
            {
                points.Add(pt);
            }
        }

        bool isInLine(Point lastPoint, Point currentPoint, Point nextPoint)
        {
            return (lastPoint.X == currentPoint.X && currentPoint.X == nextPoint.X) || (lastPoint.Y == currentPoint.Y && currentPoint.Y == nextPoint.Y);
        }

        public virtual string Url { get; set; }

        string author;
        public string Author
        {
            get { return author; }
            set { author = value; OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Author")); }
        }

        bool useShortText = false;
        public bool UseShortText { get { return useShortText; } }

        public string RealPoints
        {
            get { return realPoints.ToString(); }
        }

        public Rect Rect { get { return rect; } }

        public void SetShortText(bool status)
        {
            useShortText = status;
            onPropertyChanged("UseShortText");
            onPropertyChanged("Text");
        }

        void onPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }

        protected void OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs propertyChangedEventArgs)
        {
            if (PropertyChanged != null) PropertyChanged(this, propertyChangedEventArgs);
        }

        public virtual Brush StrokeBrush
        {
            get
            {
                if (isSelected)
                {
                    return new SolidColorBrush(Colors.Red);
                }
                else
                {
                    return new SolidColorBrush(Colors.Purple);
                }
            }
        }

        Rect rect;

        public Block(string path, string firstType)
        {
            this.firstType = firstType;

            this.realPoints.Load(path);


            updateRect();
            updatePoints();
        }

        private void updateRect()
        {
            if (realPoints.Count == 0) return;

            //if (sPoints == null) return;
            //if (string.IsNullOrEmpty(sPoints)) return;

            //List<Point> pc = getPoints(sPoints);

            double xLeft = realPoints[0].X;
            double xRight = xLeft;
            double yTop = realPoints[0].Y;
            double yBottom = yTop;

            foreach (Point p in realPoints)
            {
                xLeft = xLeft < p.X ? xLeft : p.X;
                xRight = xRight > p.X ? xRight : p.X;
                yTop = yTop < p.Y ? yTop : p.Y;
                yBottom = yBottom > p.Y ? yBottom : p.Y;
            }

            rect = new Rect(new Point(xLeft, yTop), new Point(xRight, yBottom));
        }



        public string Points
        {
            get
            {
                return points.ToString();
            }
        }

        public void updatePoints()
        {
            this.points.Clear();

            foreach (Point p in realPoints)
            {
                points.Add(new Point(p.X * rate, p.Y * rate));
            }

            onPropertyChanged("Points");
        }

        MyPointCollection points = new MyPointCollection();

        StringBuilder text = new StringBuilder();

        internal void Add(string content, int fontHeight)
        {
            contents.Add(new Content(content, fontHeight));
        }

        protected List<Content> contents = new List<Content>();

        List<IBlock> items = new List<IBlock>();

        public ICollection<IBlock> Items { get { return items; } }

        public void SetRate(double rate)
        {
            this.rate = rate;
            updatePoints();

        }

        #region INotifyPropertyChanged 成员

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IBlock 成员

        public virtual string Text
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach (Content c in contents)
                {
                    sb.AppendLine(c.Text);
                }

                return sb.ToString();
            }
            set { }
        }

        bool isSelected = false;

        public bool IsSelected
        {
            get
            {
                return isSelected;
            }
            set
            {
                isSelected = value;

                onPropertyChanged("StrokeBrush");
            }

        }

        public double ZIndex
        {
            get
            {
                return rect.Width * rect.Height;
            }
        }

        #endregion

        #region IBlock 成员


        public abstract XElement ToXElement();

        #endregion
    }

    public class Paragraph : Block, System.ComponentModel.INotifyPropertyChanged
    {
        public Paragraph(string path)
            : base(path, "paragraph")
        {
        }

        public override XElement ToXElement()
        {
            XElement xBlock = new XElement("BlockInfo", new XCData(contents[0].Text));
            xBlock.SetAttributeValue("Type", "paragraph");
            xBlock.SetAttributeValue("Path", RealPoints);
            xBlock.SetAttributeValue("FontHeight", contents[0].FontHeight);

            return xBlock;
        }
    }

    public class PageInfo : System.ComponentModel.INotifyPropertyChanged
    {
        double width;
        double height;
        string imgPath;
        double rate = 0.08;
        Size minSize;

        public double Rate { get { return rate; } }

        public PageInfo(double minWidth, double minHeight, double width, double height, string imgPath)
        {
            minSize = new Size(minWidth, minHeight);

            this.width = width;
            this.height = height;
            this.imgPath = imgPath;

            this.rate = minRate;

            selectedItems = new CollectArticInfo(rate);

            raisePropertyChanged("SelectedItem");

            raisePropertyChanged("AbsoluteImagePath");
        }

        public void Resize(double minWidth, double minHeight)
        {
            minSize = new Size(minWidth, minHeight);
        }

        ContentInfo articOrder = new ContentInfo();

        /// <summary>
        /// 稿件顺序
        /// </summary>
        public ContentInfo ArticOrder
        {
            get { return articOrder; }
            set { articOrder = value; raisePropertyChanged("ArticOrder"); }
        }


        ContentInfo pageIndex = new ContentInfo();

        /// <summary>
        /// 版次
        /// </summary>
        public ContentInfo PageIndex
        {
            get { return pageIndex; }
            set { pageIndex = value; raisePropertyChanged("PageIndex"); }
        }

        ContentInfo pageName = new ContentInfo();

        /// <summary>
        /// 版名
        /// </summary>
        public ContentInfo PageName
        {
            get { return pageName; }
            set { pageName = value; raisePropertyChanged("PageName"); }
        }

        double minRate
        {
            get
            {
                double r1 = minSize.Width / width;
                double r2 = minSize.Height / height;

                return r1 < r2 ? r1 : r2;
            }
        }

        public double Width
        {
            get { return width * rate; }
        }

        public double Height
        {
            get { return height * rate; }
        }

        public double SetRate(double rate)
        {
            rate = minRate < rate ? rate : minRate;

            this.rate = rate;

            raisePropertyChanged("Width");
            raisePropertyChanged("Height");

            foreach (IBlock i in Items)
            {
                i.SetRate(rate);
            }


            selectedItems.SetRate(rate);

            return rate;
        }

        public string ImagePath
        {
            get { return imgPath; }
        }

        public string Url
        {
            get
            {
                return GetUrl.GetAbsoluteUrl(ImagePath);
            }
            set { }
        }

        public SortedObservableItems items = new SortedObservableItems();

        public SortedObservableItems Items
        {
            get
            {
                return items;
            }
        }

        public IBlock Combine()
        {
            IBlock b = selectedItems.Combine();

            //selectedItem = new CollectArticInfo(rate);

            items.Add(b);

            foreach (IBlock bk in b.Items)
            {
                bk.IsSelected = false;
                items.Remove(bk);
            }


            raisePropertyChanged("SelectedItem");

            return b;
        }

        public void Splite()
        {
            if (selectedItems.Items.Count == 1)
            {
                IBlock i = selectedItems.Items.First();

                if (i.Items.Count == 0) return;

                foreach (IBlock iSub in i.Items)
                {
                    iSub.SetRate(rate);
                    items.Add(iSub);
                }

                items.Remove(i);

                selectedItems.Clear();
            }
        }

        ContentInfo paperInfo = new ContentInfo();

        public ContentInfo PaperInfo
        {
            get { return paperInfo; }
            set { paperInfo = value; raisePropertyChanged("PaperInfo"); }
        }


        DateTime publishData = DateTime.Today;

        public DateTime PublishData
        {
            get { return publishData; }
            set { publishData = value; }
        }

        string publishId = "1";

        public string PublishId
        {
            get { return publishId; }
            set { publishId = value; }
        }

        CollectArticInfo selectedItems;

        public IBlock SelectedItem
        {
            get
            {
                if (selectedItems.Items.Count == 1)
                {
                    return selectedItems.Items.First();
                }

                return null;
            }
        }

        public CollectArticInfo SelectedItems
        {
            get { return selectedItems; }
        }

        internal void ChangeView(double rate)
        {
            this.rate = rate;
        }

        internal static PageInfo Load(string xmlString, double minWidth, double minHeight)
        {
            TextReader tr = new StringReader(xmlString);
            XDocument x = XDocument.Load(tr);

            int pInfoWidth = (int)x.Element("PageInfoFile").Attribute("Width");
            int pInfoHeight = (int)x.Element("PageInfoFile").Attribute("Height");
            string pInfoImagePath = (string)x.Element("PageInfoFile").Attribute("url");

            PageInfo pInfo = new PageInfo(minWidth, minHeight, pInfoWidth, pInfoHeight, pInfoImagePath);

            var a = from item in x.Element("PageInfoFile").Elements() select item;

            foreach (XElement xl in a)
            {
                string path = (string)xl.Attribute("Path");
                string firstType = (string)xl.Attribute("Type");

                switch (firstType.Trim().ToLower())
                {
                    case "title":
                        {
                            string title = (string)xl.Element("Title");
                            int titleHeight = (int)xl.Element("Title").Attribute("FontHeight");
                            string subTitle = (string)xl.Element("SubTitle");
                            int subTitleHeight = (int)xl.Element("SubTitle").Attribute("FontHeight");
                            Block b = new Title(path);

                            b.Add(title, titleHeight);
                            b.Add(subTitle, subTitleHeight);

                            b.SetRate(pInfo.rate);
                            pInfo.items.Add(b);
                        }
                        break;
                    case "paragraph":
                        {
                            string paragraph = xl.Value.Trim();
                            int paragraphHeight = (int)xl.Attribute("FontHeight");
                            Block b = new Paragraph(path);
                            b.Add(paragraph, paragraphHeight);
                            b.SetRate(pInfo.rate);
                            pInfo.items.Add(b);
                        }
                        break;
                    case "image":
                        {
                            string url = (string)xl;
                            Block b = new Image(path, url);
                            b.SetRate(pInfo.rate);
                            pInfo.items.Add(b);
                        }
                        break;
                }



            }
            return pInfo;
        }

        #region INotifyPropertyChanged 成员

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        #endregion

        void raisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }

        public string ToXml()
        {
            throw new NotImplementedException();
        }

        public virtual XDocument ToXElement()
        {
            XDocument xDoc = new XDocument();
            XElement xRoot = new XElement("PageInfoFile");
            xDoc.Add(xRoot);

            xRoot.SetAttributeValue("Version", "2.0");
            xRoot.SetAttributeValue("Width", width);
            xRoot.SetAttributeValue("Height", height);
            xRoot.SetAttributeValue("url", ImagePath);

            //版名
            xRoot.SetAttributeValue("PageName", PageName);
            xRoot.SetAttributeValue("PageNameId", ContentInfo.GetId(PageName));

            xRoot.SetAttributeValue("PageIndex", PageIndex);
            xRoot.SetAttributeValue("PageIndexId", ContentInfo.GetId(PageIndex));

            xRoot.SetAttributeValue("ArticOrder", ArticOrder);
            xRoot.SetAttributeValue("ArticOrderId", ContentInfo.GetId(ArticOrder));

            //报纸名
            xRoot.SetAttributeValue("PaperName", PaperInfo);
            xRoot.SetAttributeValue("PaperId", ContentInfo.GetId(PaperInfo));

            xRoot.SetAttributeValue("PublishData", PublishData.ToShortDateString());
            xRoot.SetAttributeValue("PublishId", PublishId);

            foreach (IBlock block in items)
            {
                xDoc.Root.Add(block.ToXElement());
            }

            return xDoc;
        }

        public void EditHotArea()
        {
            throw new NotImplementedException();
        }

      
    }

    /// <summary>
    /// 内容
    /// </summary>
    public class Content
    {
        public Content() { }
        public Content(string text, int fontHeight)
        {
            this.Text = text;
            this.FontHeight = fontHeight;
        }

        public bool IsEmpty
        {
            get
            {
                return string.IsNullOrEmpty(text);
            }
        }

        string text = string.Empty;

        public string Text
        {
            get { return text; }
            set { text = value; }
        }
        public int FontHeight { get; set; }

        public override string ToString()
        {
            return Text;
        }
    }

    public class SortedObservableItems : System.Collections.ObjectModel.ObservableCollection<IBlock>
    {
        public new void Add(IBlock block)
        {
            int index = GetInsertIndex(block);
            base.Insert(index, block);
        }

        int GetInsertIndex(IBlock block)
        {
            int index = 0;
            foreach (IBlock b in this)
            {
                if (block.ZIndex > b.ZIndex)
                {
                    return index;
                }
                index++;
            }

            return index;
        }
    }

    public class MyPointCollection : List<Point>
    {
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            foreach (Point pt in this)
            {
                sb.Append((int)pt.X + "," + (int)pt.Y + " ");
            }

            return sb.ToString().Trim();
        }

        public void Load(string path)
        {
            this.Clear();
            string[] parts = path.Split(' ');
            foreach (string s in parts)
            {
                string[] pt = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                this.Add(new Point(Convert.ToDouble(pt[0]), Convert.ToDouble(pt[1])));
            }

        }
    }

    public class ImageCollection : List<IBlock>
    {
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (IBlock i in this)
            {
                PropertyInfo pInfo = i.GetType().GetProperty("Url");
                if (pInfo != null)
                {
                    string s = Convert.ToString(pInfo.GetValue(i, null));
                    if (!string.IsNullOrEmpty(s))
                    {
                        sb.Append(s + ";");
                    }
                }
            }
            return sb.ToString().TrimEnd(';');
        }
    }

    public class CollectArticInfo : System.ComponentModel.INotifyPropertyChanged
    {
        public CollectArticInfo(double rate)
        {
            this.rate = rate;
        }

        Content title = new Content();
        public Content Title
        {
            get { return title; }
            set { title = value; }
        }


        T GetValue<T>(string propertyName)
        {
            if (items.Count == 1)
            {
                PropertyInfo pInfo = items.First().GetType().GetProperty(propertyName);
                if (pInfo != null)
                {
                    return (T)pInfo.GetValue(items.First(), null);
                }
            }
            return default(T);
        }

        void SetValue<T>(string propertyName, object value)
        {
            if (items.Count == 1)
            {
                PropertyInfo pInfo = items.First().GetType().GetProperty(propertyName);

                if (pInfo != null)
                {
                    if (value != null)
                    {
                        pInfo.SetValue(items.First(), value, null);
                    }
                }

                raisePropertyChanged(propertyName);
            }
        }

        Content subTitle = new Content();

        public Content SubTitle
        {
            get { return subTitle; }
            set { subTitle = value; }
        }

        Content introTitle = new Content();
        public Content IntroTitle
        {
            get { return introTitle; }
            set { introTitle = value; }
        }

        string contents;
        public string Contents
        {
            get { return contents; }
            set
            {
                contents = value;
                Changed = true;
            }
        }

        bool changed = false;

        public bool Changed
        {
            get { return changed; }
            private set { changed = value; }
        }

        public string Url
        {
            get
            {
                return images.ToString();
            }
        }

        ImageCollection images = new ImageCollection();

        public ImageCollection Images { get { return images; } }


        public ContentInfo ArticProperty
        {
            get { return (ContentInfo)getContentInfo("ArticProperty"); }
            set
            {
                setContentInfo("ArticProperty", value);
                updateContents();
            }
        }

        Rect rect;

        public Rect Rect { get { return rect; } }

        bool isSelected = false;

        public bool IsReadOnly
        {
            get
            {
                //return items.Count != 1 || !(items[0] is ArticInfo);
                return false;
            }
        }

        public string Author
        {
            get
            {
                return (string)getContentInfo("Author");
            }
            set
            {
                setContentInfo("Author", value);
            }
        }


        public ContentInfo ArticType
        {
            get
            {
                return (ContentInfo)getContentInfo("ArticType");
            }
            set
            {
                setContentInfo("ArticType", value);
            }
        }

        object getContentInfo(string propertyName)
        {
            if (items.Count == 1)
            {
                object aInfo = items.First();

                if (aInfo != null)
                {
                    PropertyInfo pInfo = aInfo.GetType().GetProperty(propertyName);
                    if (pInfo != null) return pInfo.GetValue(aInfo, null);
                }
            }
            return null;
        }

        void setContentInfo(string propertyName,object value)
        {
            if (items.Count == 1)
            {
                object aInfo = items.First();

                if (aInfo != null)
                {
                    PropertyInfo pInfo = aInfo.GetType().GetProperty(propertyName);

                    if (pInfo != null)
                    {
                        pInfo.SetValue(aInfo, value, null);
                        raisePropertyChanged(propertyName);
                    }
                }
            }
        }

        /// <summary>
        /// 转版信息
        /// </summary>
        public ContentInfo NextPage
        {
            get
            {
                return (ContentInfo)getContentInfo("NextPage");
            }
            set
            {
                setContentInfo("NextPage", value);
            }
        }

        /// <summary>
        /// 稿件来源
        /// </summary>
        public ContentInfo ArticSource
        {
            get
            {
                return (ContentInfo)getContentInfo("ArticSource");
            }
            set
            {
                setContentInfo("ArticSource", value);
            }
        }

        /// <summary>
        /// 合并
        /// 
        /// </summary>
        /// <returns></returns>
        public IBlock Combine()
        {
            if (items.Count > 1)
            {
                updateContents();
            }

            ArticInfo aInfo = new ArticInfo(rate);
            aInfo.Title = this.Title;
            aInfo.SubTitle = this.SubTitle;
            aInfo.IntroTitle = this.IntroTitle;
            aInfo.Contents = this.Contents;
            aInfo.Images.AddRange(this.images);
            aInfo.Author = this.Author;

            ////如果有两个内容，并且第一个是图片；如果第一个是图片，则设定为图文说明
            //if(items.Count == 2 && items.First() is Image)
            //{
            //    var a = from n in DataSourceControl.GetCollection(DictionaryInfo.ArticProperty) where ContentInfo.GetInt32Id(n) == (int)ArticPropertyEnum.图文说明 select n;

            //    aInfo.ArticProperty = a.First();
            //}

            foreach (IBlock b in this.items)
            {
                aInfo.Items.Add(b);
            }
            aInfo.updatePoints();

            this.Clear();

            return aInfo;
        }

        public void Clear()
        {
            images.Clear();

            foreach (IBlock i in items)
            {
                i.IsSelected = false;
            }
            items.Clear();

            updateContents();
            raisePropertyChanged("IsReadOnly");
        }

        #region INotifyPropertyChanged 成员

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        #endregion

        List<IBlock> items = new List<IBlock>();

        public ICollection<IBlock> Items
        {
            get { return items; }
        }

        public void Add(IBlock block)
        {
            items.Add(block);
            updateContents();

            raisePropertyChanged("IsReadOnly");
            raisePropertyChanged("Points");
            raisePropertyChanged("Author");
            raisePropertyChanged("ArticType");
            raisePropertyChanged("NextPage");
            raisePropertyChanged("ArticProperty");
            raisePropertyChanged("ArticSource");
            raisePropertyChanged("ArticType");
        }

        public void Remove(IBlock block)
        {
            items.Remove(block);

            updateContents();

            raisePropertyChanged("IsReadOnly");
            raisePropertyChanged("Points");
            raisePropertyChanged("Author");
            raisePropertyChanged("ArticType");
            raisePropertyChanged("NextPage");
            raisePropertyChanged("ArticProperty");
            raisePropertyChanged("ArticSource");
            raisePropertyChanged("ArticType");
        }

        class TitleCollection : List<Content>
        {

            public void Update()
            {
                if (this.Count >= 3)
                {
                    //字体最大的为引题
                    IntroTitle = popBiggest();
                    Title = popBiggest();
                    SubTitle = popBiggest();

                    paragraphs.AddRange(this);

                    this.Clear();
                }
                else if (this.Count == 2)
                {
                    Title = popBiggest();
                    SubTitle = popBiggest();
                }
                else
                {
                    Title = popBiggest();
                }
            }

            Content introTitle = new Content();
            public Content IntroTitle
            {
                get { return introTitle; }
                set { introTitle = value; }
            }
            Content subTitle = new Content();
            public Content SubTitle
            {
                get { return subTitle; }
                set { subTitle = value; }
            }
            Content title = new Content();
            public Content Title
            {
                get { return title; }
                set { title = value; }
            }

            List<Content> paragraphs = new List<Content>();

            public ICollection<Content> Paragraphs
            {
                get { return paragraphs; }
            }

            Content popBiggest()
            {
                if (this.Count <= 0) return null;

                Content introTitle = this.First();

                foreach (Content c in this)
                {
                    if (c.FontHeight > introTitle.FontHeight)
                    {
                        introTitle = c;
                    }
                }

                this.Remove(introTitle);

                return introTitle;
            }
        }

        protected void updateContents()
        {
            Title = new Content();
            SubTitle = new Content();
            IntroTitle = new Content();

            Images.Clear();

            StringBuilder contents = new StringBuilder();

            foreach (IBlock b in items)
            {
                switch (b.GetType().Name)
                {
                    case "Image":
                        {
                            Images.Add(b);
                        }
                        break;
                    case "Title":
                        {
                            Title bT = b as Title;

                            TitleCollection tContents = new TitleCollection();

                            if (this.Title != null && !this.Title.IsEmpty ) tContents.Add(this.Title);

                            if (this.IntroTitle != null && !this.Title.IsEmpty) tContents.Add(this.IntroTitle);

                            if (this.SubTitle != null && !this.Title.IsEmpty) tContents.Add(this.SubTitle);

                            if (bT.IntroTitle != null && !bT.IntroTitle.IsEmpty) tContents.Add(bT.IntroTitle);

                            if (bT.SubTitle != null && !bT.SubTitle.IsEmpty) tContents.Add(bT.SubTitle);

                            tContents.Add(new Content(bT.Text, bT.FontHeight));

                            tContents.Update();
                            IntroTitle = tContents.IntroTitle;
                            SubTitle = tContents.SubTitle;
                            Title = tContents.Title;

                            foreach (Content c in tContents.Paragraphs)
                            {
                                contents.AppendLine(c.Text);
                            }
                        }
                        break;
                    case "Paragraph":
                        {
                            contents.AppendLine(b.Text);
                        }
                        break;
                    case "ArticInfo":
                        {
                            ArticInfo a = b as ArticInfo;

                            switch ((ArticPropertyEnum)ContentInfo.GetInt32Id(a.ArticProperty))
                            {
                                case  ArticPropertyEnum.文章块://ArticProperty.文章块:
                                    {
                                        Title = a.Title;
                                        SubTitle = a.SubTitle;
                                        IntroTitle = a.IntroTitle;
                                        Images.AddRange(a.Images);
                                        contents.AppendLine(a.Contents);
                                    }
                                    break;
                                case  ArticPropertyEnum.图文说明://ArticProperty.图文说明:
                                    {
                                        Images.Add(a);

                                        if (items.Count == 1)
                                        {
                                            contents.AppendLine(a.Contents);
                                        }
                                        //contents.AppendLine(a.Contents);
                                    }
                                    break;
                                default:
                                    break;
                            }

                        }
                        break;
                    default:
                        break;
                }
            }



            this.contents = contents.ToString();


            raisePropertyChanged("Title");
            raisePropertyChanged("SubTitle");
            raisePropertyChanged("IntroTitle");
            raisePropertyChanged("Images");
            raisePropertyChanged("Contents");
            raisePropertyChanged("Url");

     
        }

        private void raisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }


        #region IBlock 成员

        public string Text
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                isSelected = value;
                raisePropertyChanged("StrokeBrush");
            }

        }

        public double ZIndex
        {
            get { return rect.Width * rect.Height; }
        }

        public string Points
        {
            get
            {
                if (items.Count == 1)
                {
                    return items.First().RealPoints;
                }

                return string.Empty;
            }
        }

        public Brush StrokeBrush
        {
            get
            {
                if (isSelected)
                {
                    return new SolidColorBrush(Colors.Red);
                }
                else
                {
                    switch ((ArticPropertyEnum)ContentInfo.GetInt32Id(ArticProperty))
                    {
                        case ArticPropertyEnum.文章块://ArticProperty.文章块:
                            return new SolidColorBrush(Colors.Green);
                        case  ArticPropertyEnum.图文说明://ArticProperty.图文说明:
                            return new SolidColorBrush(Colors.Yellow);
                        default:
                            return new SolidColorBrush(Colors.Purple);

                    }
                }
            }
        }

        #region 生成XML
        public virtual XElement ToXElement()
        {
            switch (ArticProperty.Id)
            {
                case "0"://ArticProperty.文章块:
                    {

                        XElement xArticle = new XElement("Article");
                        xArticle.SetAttributeValue("Detail", "true");
                        xArticle.SetAttributeValue("Id", "8");
                        xArticle.SetAttributeValue("Type", "");
                        xArticle.SetAttributeValue("AdId", "");


                        XElement xIntroTitle = new XElement("IntroTitle", new XCData(IntroTitle.Text));
                        xArticle.Add(xIntroTitle);


                        XElement xTitle = new XElement("Title", new XCData(Title.Text));
                        xTitle.SetAttributeValue("Font", "微软雅黑");
                        xTitle.SetAttributeValue("Size", "36");
                        xTitle.SetAttributeValue("Bold", "true");
                        xTitle.SetAttributeValue("Italic", "false");
                        xTitle.SetAttributeValue("Align", "justify");
                        xTitle.SetAttributeValue("LetterSpacing", "-2");
                        xTitle.SetAttributeValue("Leading", "-6");
                        xTitle.SetAttributeValue("Txt", "");
                        xArticle.Add(xTitle);

                        XElement xSubTitle = new XElement("SubTitle", new XCData(SubTitle.Text));
                        xArticle.Add(xSubTitle);

                        XElement xAuthor = new XElement("Author");
                        xArticle.Add(xAuthor);

                        XElement xContent = new XElement("Content", new XCData(Contents));
                        xContent.SetAttributeValue("Font", "方正报宋简体");
                        xContent.SetAttributeValue("Size", "16");
                        xContent.SetAttributeValue("Bold", "true");
                        xContent.SetAttributeValue("Italic", "false");
                        xContent.SetAttributeValue("Align", "left");
                        xContent.SetAttributeValue("LetterSpacing", "0");
                        xContent.SetAttributeValue("Leading", "8");
                        xArticle.Add(xContent);

                        XElement xImageList = new XElement("ImageList");
                        xImageList.SetAttributeValue("ID", "0");
                        xImageList.SetAttributeValue("X", "0");
                        xImageList.SetAttributeValue("Y", "0");

                        xArticle.Add(xImageList);

                        foreach (IBlock b in Images)
                        {
                            xImageList.Add(b.ToXElement());
                        }

                        XElement xPointList = new XElement("PointList",
                            new XElement("Point", new XAttribute("X", "19"), new XAttribute("Y", "102")),
                            new XElement("Point", new XAttribute("X", "699"), new XAttribute("Y", "102")),
                            new XElement("Point", new XAttribute("X", "699"), new XAttribute("Y", "462")),
                            new XElement("Point", new XAttribute("X", "19"), new XAttribute("Y", "462")),
                            new XElement("Point", new XAttribute("X", "19"), new XAttribute("Y", "102")));
                        xArticle.Add(xPointList);


                        return xArticle;
                    }
                case "1"://ArticProperty.图文说明:
                    {
                        XElement xImage = new XElement("Image", new XCData(this.Contents));
                        xImage.SetAttributeValue("PathName", Url);
                        xImage.SetAttributeValue("X", "0");
                        xImage.SetAttributeValue("Y", "0");
                        xImage.SetAttributeValue("Author", Author);

                        return xImage;
                    }
                case "2"://ArticProperty.转换为图片:
                    break;
                case "3"://ArticProperty.文字打印到图片:
                    break;
                default:
                    break;
            }

            return null;
        }
        #endregion

        double rate;

        public void SetRate(double rate)
        {
            this.rate = rate;
            updatePoints();
        }

        #endregion

        public void updatePoints()
        {
            double xLeft = double.NaN;
            double xRight = double.NaN;
            double yTop = double.NaN;
            double yBottom = double.NaN;

            int index = 0;
            foreach (IBlock i in items)
            {
                if (index == 0)
                {
                    xLeft = i.Rect.Left;
                    xRight = i.Rect.Right;
                    yTop = i.Rect.Top;
                    yBottom = i.Rect.Bottom;
                }
                else
                {
                    xLeft = xLeft < i.Rect.Left ? xLeft : i.Rect.Left;
                    xRight = xRight > i.Rect.Right ? xRight : i.Rect.Right;
                    yTop = yTop < i.Rect.Top ? yTop : i.Rect.Top;
                    yBottom = yBottom > i.Rect.Bottom ? yBottom : i.Rect.Bottom;
                }

                index++;
            }

            rect = new Rect(new Point(xLeft, yTop), new Point(xRight, yBottom));

            //points.Clear();
            //points.Add(new Point(rect.Left * rate, rect.Top * rate));
            //points.Add(new Point(rect.Right * rate, rect.Top * rate));
            //points.Add(new Point(rect.Right * rate, rect.Bottom * rate));
            //points.Add(new Point(rect.Left * rate, rect.Bottom * rate));

            raisePropertyChanged("Points");
        }
    }

    public enum ArticPropertyEnum { 文章块=141, 图文说明, 转换为图片, 文字打印到图片 }
    public enum NextPageTypeEnum { 无转版, 下转, 上接 }
    public enum ArticOrderTypeEnum { 按坐标位置排序, 按合并顺序排序 }


    /// <summary>
    /// 信息
    /// </summary>
    public class ContentInfo
    {
        public string Id { get; set; }

        public string Name { get; set; }

        public override string ToString()
        {
            return Name;
        }

        public static string GetId(ContentInfo cInfo)
        {
            if (cInfo != null) return cInfo.Id;
            return "0";
        }

        public static int GetInt32Id(ContentInfo cInfo)
        {
            if (cInfo != null) return Convert.ToInt32(cInfo.Id);
            return 0;
        }
    }

    public class ArticTypeContentInfo : ContentInfo
    {

    }

    public enum DictionaryInfo
    {
        /// <summary>
        /// 版名
        /// </summary>
        PageName = 94,
        /// <summary>
        /// 版次
        /// </summary>
        PageIndex = 117,
        /// <summary>
        /// 稿件分类
        /// </summary>
        ArticType = 15,
        /// <summary>
        /// 稿件来源
        /// </summary>
        ArticSource = 130,
        /// <summary>
        /// 稿件属性
        /// </summary>
        ArticProperty = 140,
        /// <summary>
        /// 稿件排序
        /// </summary>
        ArticOrder = 149,
        /// <summary>
        /// 转版
        /// </summary>
        NextPage = 145,
        /// <summary>
        /// 报纸
        /// </summary>
        PaperInfo = -100
    }



    public class GetUrl
    {
        public static string GetAbsoluteUrl(string strRelativePath)
        {
            strRelativePath = formatePath(strRelativePath);

            if (string.IsNullOrEmpty(strRelativePath))
            {
                return "Images/empty.jpg";
            }

            string strFullUrl;

            if (strRelativePath.StartsWith("http:", StringComparison.OrdinalIgnoreCase)
              || strRelativePath.StartsWith("https:", StringComparison.OrdinalIgnoreCase)
              || strRelativePath.StartsWith("file:", StringComparison.OrdinalIgnoreCase)
              )
            {
                // 已经是绝对路径。
                strFullUrl = strRelativePath;
            }
            else
            {
                // 是相对路径，必须转换成绝对路径。
                strFullUrl = System.Windows.Application.Current.Host.Source.AbsoluteUri;
                if (strFullUrl.IndexOf("ClientBin") > 0)
                    strFullUrl = strFullUrl.Substring(0, strFullUrl.IndexOf("ClientBin")) + strRelativePath;
                else
                    strFullUrl = strFullUrl.Substring(0, strFullUrl.LastIndexOf("/") + 1) + strRelativePath;
            }

            return strFullUrl;
        }

        static string formatePath(string path)
        {
            if (string.IsNullOrEmpty(path)) return null;

            return path.Replace('\\', '/');
        }
    }
}
